<?php

namespace app\common\model\task;

use app\admin\model\Admin;
use app\admin\model\admin\Info;
use app\admin\model\finance\extract\Week;
use app\admin\model\School;
use think\Model;

class Outer extends Model
{
    // 表名
    protected $name = 'finance_extract_task_outer';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    // 追加属性
    protected $append = [
        'userid_text',
        'week_id_text',
        'school_id_text',
        'status_text',
        'stateclass',
        'external_text',
        'externalimg_text',
        'createtime_text'
    ];

    public function getCreatetimeTextAttr($value, $data)
    {
        return date('Y-m-d H:i:s',$data['createtime']);
    }

    public function getUseridTextAttr($value, $data)
    {
        $admin = Admin::get(['userid'=>$data['userid']]);
        return $admin['username'];
    }

    public function getExternalTextAttr($value, $data)
    {
        $admin = Admin::get(['userid'=>$data['external']]);
        return $admin['username'];
    }

    public function getExternalimgTextAttr($value, $data)
    {
        $admin = Admin::get(['userid'=>$data['external']]);
        return $admin['avatar'];
    }

    public function getWeekIdTextAttr($value, $data)
    {
        $week = Week::get($data['week_id']);
        return $week['name'];
    }

    public function getSchoolIdTextAttr($value, $data)
    {
        $school = School::get($data['school_id']);
        return $school['name'];
    }

    public function getStatusTextAttr($value, $data)
    {
        $status = [0=>'新发布',1=>'审核中',2=>'审核通过',-1=>'已驳回',3=>'已接收',4=>'已作废'];
        return $status[$data['status']];
    }

    public function getStateclassAttr($value, $data)
    {
        //样式
        $status = [0=>'fabu',1=>'liucheng',3=>'pass',2=>'finish',-1=>'bohui',4=>'zuofei'];
        return $status[$data['status']];
    }

    /**
     * 获取任务单
     */
    public static function getOuter($weekid)
    {
        //统计已发布的任务单
        $stat = Outer::where('week_id',$weekid)->count();
        //统计通过的任务单  2
        $pass = Outer::where(['week_id'=>$weekid,'status'=>2])->count();
        //统计未审核通过任务单-1 4
        $notAudit= Outer::where("week_id={$weekid} and (status=-1 or status=4)")->count();
        //未提交审核的任务单0-3
        $not = Outer::where("week_id={$weekid} and (status=0 or status=3)")->count();
        //审核中  
        $audit = Outer::where(['week_id'=>$weekid,'status'=>1])->count();
        return [$stat,$pass,$notAudit,$not,$audit];
    }
}
